package leetcode;

/**
 * Created by Ghost on 2014/11/22.
 */
public class Sum_Root_to_Leaf_Numbers {
    private int sum = 0;

    private void findAndAdd(TreeNode root, int upLevelSum) {
        if(root!=null){
            if (root.left == null && root.right==null) {
                sum += 10*upLevelSum+root.val;
            } else {
                findAndAdd(root.left,10*upLevelSum+root.val);
                findAndAdd(root.right,10*upLevelSum+root.val);
            }
        }
    }

    public int sumNumbers(TreeNode root) {
        findAndAdd(root,0);
        return sum;
    }
    public static void main(String[] args){
        Sum_Root_to_Leaf_Numbers sum_root_to_leaf_numbers = new Sum_Root_to_Leaf_Numbers();
        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(2);
        root.left.right = new TreeNode(3);
        System.out.println(sum_root_to_leaf_numbers.sumNumbers(null));
    }
}
